package com.anhry.www.safety.manage.zczb.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Service;

import com.anhry.www.safety.manage.zczb.bean.TbZczbBgd;
import com.anhry.www.safety.manage.zczb.bean.TbZczbBzhType;
import com.anhry.www.safety.manage.zczb.bean.TbZczbHzd;
import com.anhry.www.safety.manage.zczb.service.ZczbService;
import com.anhry.www.safety.util.Loggers;
import com.anhry.www.safety.util.dao.UtilDao;

/**
 * @date:2012-9-27
 * @remark:
 * @author:dongbin
 */

@Service
@SuppressWarnings("unchecked")
public class ZczbServiceImpl implements ZczbService{
	
	@Resource
	private UtilDao utilDao;

	public UtilDao getUtilDao() {
		return utilDao;
	}

	public void setUtilDao(UtilDao utilDao) {
		this.utilDao = utilDao;
	}

	public List getYhList(String corpId, String hzdId, String bgdId,String cType,String cType1,String cZg) throws Exception {
		StringBuilder hql = new StringBuilder("from TbZczbYhAllView t where 1=1 ");
		if(null!=corpId && corpId.length()>0){
			hql.append(" and t.corpId="+corpId);
		}
		if(null!=hzdId && hzdId.length()>0){
			hql.append(" and t.hzdId="+hzdId);
		}
		if(null!=bgdId && bgdId.length()>0){
			hql.append(" and t.bgdId="+bgdId);
		}
		if(null!=cType && cType.length()>0){
			if("1".equals(cType) || "2".equals(cType)){
				hql.append(" and t.yhType1="+cType);
			}else{
				hql.append(" and t.yhType2="+cType);
			}
		}
		if(null!=cType1 && cType1.length()>0){
			hql.append(" and t.yhType="+cType1);
		}
		if(null!=cZg && cZg.length()>0){
			hql.append(" and t.yhIsZg="+cZg);
		}
		hql.append(" order by t.yhType1,t.yhType2,t.yhInfoId");
		return (List)this.utilDao.getHqlQuery(hql.toString());
	}
	
	public List getYhList(String corpId,String bgdId,String cType) throws Exception {
		StringBuilder hql = new StringBuilder("from TbZczbYhAllView t where 1=1 ");
		if(null!=bgdId && bgdId.length()>0){
			hql.append(" and t.bgdId="+bgdId);
		}else{
			if(null!=corpId && corpId.length()>0){
				hql.append(" and t.corpId="+corpId);
			}
			hql.append(" and t.bgdId is null");
		}
		if(null!=cType && cType.length()>0){
			if("1".equals(cType) || "2".equals(cType)){
				hql.append(" and t.yhType1="+cType);
			}else{
				hql.append(" and t.yhType2="+cType);
			}
		}
		hql.append(" order by t.yhType1,t.yhType2,t.yhInfoId");
		return (List)this.utilDao.getHqlQuery(hql.toString());
	}

	public List getYhFjList(String fjType, String yhId) throws Exception {
		StringBuilder hql = new StringBuilder("from TbZczbYhFj t where 1=1 ");
		if(null!=fjType && fjType.length()>0){
			hql.append(" and t.fjType="+fjType);
		}
		if(null!=yhId && yhId.length()>0){
			hql.append(" and t.yhId="+yhId);
		}
		hql.append(" order by t.id");
		return (List)this.utilDao.getHqlQuery(hql.toString());
	}

	public String getItemXml(String hzdId,String bgdId,String yhType,String isZg) throws Exception {
		String tem = this.typeTreeInit(hzdId,bgdId,yhType,isZg).asXML();
		tem = tem.replaceAll("\\\"", "\\\\\"");
		tem = tem.replaceAll("\n", "");
		return tem;
	}
	
	public Document typeTreeInit(String hzdId,String bgdId,String yhType,String isZg){
		try {
			String t1 = "0";
			String t2 = "0";
			List tList = null;
			StringBuilder sql1 = new StringBuilder("select count(*) from tb_zczb_yh_all_view t where 1=1 and t.yh_type1=1");
			StringBuilder sql2 = new StringBuilder("select count(*) from tb_zczb_yh_all_view t where 1=1 and t.yh_type1=2");
			StringBuilder sql3 = new StringBuilder("select t.yh_type2, count(*) from tb_zczb_yh_all_view t where 1=1");
			if(null!=hzdId && hzdId.length()>0){
				sql1.append(" and t.hzd_id="+hzdId+"");
				sql2.append(" and t.hzd_id="+hzdId+"");
				sql3.append(" and t.hzd_id="+hzdId+"");
				
			}
			if(null!=bgdId && bgdId.length()>0){
				sql1.append(" and t.bgd_id="+bgdId+"");
				sql2.append(" and t.bgd_id="+bgdId+"");
				sql3.append(" and t.bgd_id="+bgdId+"");
				
			}
			if(null!=yhType && yhType.length()>0){
				sql1.append(" and t.yh_type="+yhType+"");
				sql2.append(" and t.yh_type="+yhType+"");
				sql3.append(" and t.yh_type="+yhType+"");
				
			}
			if(null!=isZg && isZg.length()>0){
				sql1.append(" and t.yh_is_zg="+isZg+"");
				sql2.append(" and t.yh_is_zg="+isZg+"");
				sql3.append(" and t.yh_is_zg="+isZg+"");
				
			}
			sql3.append(" group by t.yh_type2");
			t1 = this.utilDao.getObjectBySql(sql1.toString());
			t2 = this.utilDao.getObjectBySql(sql2.toString());
			tList = this.utilDao.getObjectBySql(sql3.toString(), 0, 0);
			Document   document = DocumentHelper.createDocument();
			Element tree = document.addElement("tree");
			tree.addAttribute("id", "0");
			Element type1= tree.addElement("item");
			if(!"0".equals(t1)){
				type1.addAttribute("text","基础管理（<font color='red'>"+t1+"</font>）");
			}else{
				type1.addAttribute("text","基础管理");
			}
			type1.addAttribute("id", "1");
			type1.addAttribute("im0", "folderClosed.gif");
			type1.addAttribute("im1", "folderOpen.gif");
			type1.addAttribute("im2", "folderClosed.gif");
			type1.addAttribute("open", "1");
			
			Element type2= tree.addElement("item");			
			if(!"0".equals(t2)){
				type2.addAttribute("text","现场管理（<font color='red'>"+t2+"</font>）");
			}else{
				type2.addAttribute("text","现场管理");
			}
			type2.addAttribute("id", "2");
			type2.addAttribute("im0", "folderClosed.gif");
			type2.addAttribute("im1", "folderOpen.gif");
			type2.addAttribute("im2", "folderClosed.gif");
			type2.addAttribute("open", "1");
			
			List allList = (List)this.utilDao.getHqlQuery("from TbZczbBzhType t where 1=1 and t.typeGrade=2 order by t.typeSort");
			if(null!=allList && allList.size()>0){
				for(int i=0;i<allList.size();i++){
					boolean b = false;
					String num = "";
					TbZczbBzhType type = (TbZczbBzhType)allList.get(i);
					if(null!=tList && tList.size()>0){
						for(int j=0;j<tList.size();j++){
							Object obj[] = (Object[])tList.get(j) ;
							if(type.getId().toString().equals(obj[0].toString())){
								b = true;
								num = obj[1].toString();
								break;
							}
						}
						if("1".equals(type.getTypeParentId().toString())){
							Element main= type1.addElement("item");
							if(b){
								main.addAttribute("text",type.getTypeName()+"（<font color='red'>"+num+"</font>）");
							}else{
								main.addAttribute("text",type.getTypeName()+"（0）");
							}
							main.addAttribute("id", type.getId().toString());
							main.addAttribute("im0", "folderClosed.gif");
							main.addAttribute("im1", "folderOpen.gif");
							main.addAttribute("im2", "folderClosed.gif");
						}else{
							Element main= type2.addElement("item");			
							if(b){
								main.addAttribute("text",type.getTypeName()+"（<font color='red'>"+num+"</font>）");
							}else{
								main.addAttribute("text",type.getTypeName()+"（0）");
							}
							main.addAttribute("id", type.getId().toString());
							main.addAttribute("im0", "folderClosed.gif");
							main.addAttribute("im1", "folderOpen.gif");
							main.addAttribute("im2", "folderClosed.gif");
						}
					}else{
						if("1".equals(type.getTypeParentId().toString())){
							Element main= type1.addElement("item");			
							main.addAttribute("text",type.getTypeName());
							main.addAttribute("id", type.getId().toString());
							main.addAttribute("im0", "folderClosed.gif");
							main.addAttribute("im1", "folderOpen.gif");
							main.addAttribute("im2", "folderClosed.gif");
						}else{
							Element main= type2.addElement("item");			
							main.addAttribute("text",type.getTypeName());
							main.addAttribute("id", type.getId().toString());
							main.addAttribute("im0", "folderClosed.gif");
							main.addAttribute("im1", "folderOpen.gif");
							main.addAttribute("im2", "folderClosed.gif");
						}
					}
				}
			}
			return document;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("ZczbServiceImpl 中 Document typeTreeInit(String hzdId,String bgdId,String yhType,String isZg)方法："+e.getMessage());
		}
		return null;
	}

	public void initBgd(String bgdId){
		int itemNum = 0;	//自查总项数
		int bigNum = 0;		//重大隐患数
		int sameNum = 0;	//一般隐患数
		int sameClNum = 0;	//一般隐患数处理数
		int bigClNum = 0;	//重大隐患数处理数
		StringBuilder sql = new StringBuilder("select count(*) from tb_zczb_yh_all_view t where 1=1 ");
		String sql0 = "";
		String sql1 = "";
		String sql2 = "";
		String sql3 = "";
		String sql4 = "";
		try {
			if(null!=bgdId && bgdId.length()>0){
				TbZczbBgd bgd = (TbZczbBgd)this.utilDao.getObjectById(TbZczbBgd.class, Long.parseLong(bgdId));
				sql0 = sql.toString()+(" and t.bgd_id="+bgdId);//一般隐患
				sql1 = sql.toString()+(" and t.yh_type=1 and t.bgd_id="+bgdId);//一般隐患
				sql2 = sql.toString()+(" and t.yh_type=2 and t.bgd_id="+bgdId);//重大隐患
				sql3 = sql.toString()+(" and t.yh_type=1 and t.yh_is_zg=1 and t.bgd_id="+bgdId);//一般隐患处理数
				sql4 = sql.toString()+(" and t.yh_type=2 and t.yh_is_zg=1 and t.bgd_id="+bgdId);//重大隐患处理数
				itemNum = Integer.parseInt(this.utilDao.getObjectBySql(sql0));
				sameNum = Integer.parseInt(this.utilDao.getObjectBySql(sql1));
				bigNum = Integer.parseInt(this.utilDao.getObjectBySql(sql2));
				sameClNum = Integer.parseInt(this.utilDao.getObjectBySql(sql3));
				bigClNum = Integer.parseInt(this.utilDao.getObjectBySql(sql4));
				bgd.setBgdItemNum(itemNum);
				bgd.setBgdSameNum(sameNum);
				bgd.setBgdBigNum(bigNum);
				bgd.setBgdYhNum(sameNum+bigNum);
				bgd.setBgdSameCl(sameClNum);
				bgd.setBgdBigCl(bigClNum);
				bgd.setBgdClNum(sameClNum+bigClNum);
				bgd.setBgdWclNum(sameNum+bigNum-sameClNum-bigClNum);
				this.utilDao.update(bgd);
				this.initHzd(bgd.getHzdId()+"");
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("ZczbServiceImpl 中 void initBgd(String bgdId)方法："+e.getMessage());
		}
	}
	
	/**
	 * 报告单信息保存
	 * @param bgd
	 * @param bgdId
	 * @return
	 */
	public TbZczbHzd initHzd(String hzdId){
		TbZczbHzd hzd = null;
		StringBuilder sql = new StringBuilder("select count(*) from tb_zczb_bgd t where 1=1 and t.hzd_id="+hzdId);//自查次数
		StringBuilder sql0 = new StringBuilder("select sum(t.bgd_item_num) from tb_zczb_bgd t where 1=1 and t.hzd_id="+hzdId);//自查项数
		StringBuilder sql1 = new StringBuilder("select sum(t.bgd_same_num) from tb_zczb_bgd t where 1=1 and t.hzd_id="+hzdId);//一般隐患数
		StringBuilder sql2 = new StringBuilder("select sum(t.bgd_big_num) from tb_zczb_bgd t where 1=1 and t.hzd_id="+hzdId);//重大隐患数
		StringBuilder sql3 = new StringBuilder("select sum(t.bgd_same_cl) from tb_zczb_bgd t where 1=1 and t.hzd_id="+hzdId);//一般隐患处理数
		StringBuilder sql4 = new StringBuilder("select sum(t.bgd_big_cl) from tb_zczb_bgd t where 1=1 and t.hzd_id="+hzdId);//重大隐患处理数
		try {
			if(null!=hzdId && hzdId.length()>0){
				hzd = (TbZczbHzd)this.utilDao.getObjectById(TbZczbHzd.class, Long.parseLong(hzdId));
				if(null!=hzd){
					hzd.setHzdBgdNum(Integer.parseInt(this.utilDao.getObjectBySql(sql.toString())));
					hzd.setHzdItemNum(Integer.parseInt(this.utilDao.getObjectBySql(sql0.toString())));
					hzd.setHzdSameNum(Integer.parseInt(this.utilDao.getObjectBySql(sql1.toString())));
					hzd.setHzdBigNum(Integer.parseInt(this.utilDao.getObjectBySql(sql2.toString())));
					hzd.setHzdYhNum(hzd.getHzdSameNum()+hzd.getHzdBigNum());
					hzd.setHzdSameCl(Integer.parseInt(this.utilDao.getObjectBySql(sql3.toString())));
					hzd.setHzdBigCl(Integer.parseInt(this.utilDao.getObjectBySql(sql4.toString())));
					hzd.setHzdClNum(hzd.getHzdSameCl()+hzd.getHzdBigCl());
					hzd.setHzdWclNum(hzd.getHzdSameNum()+hzd.getHzdBigNum()-hzd.getHzdSameCl()-hzd.getHzdBigCl());
					this.utilDao.update(hzd);
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("ZczbServiceImpl 中 TbZczbHzd initHzd(String hzdId)方法："+e.getMessage());
		}
		return hzd;
	}

	public List getXwYhList(String corpId, String bgdId) throws Exception {
		StringBuilder hql = new StringBuilder("from TbZczbXwYh t where 1=1 ");
		if(null!=corpId && corpId.length()>0){
			hql.append(" and t.corpId="+corpId);
		}
		if(null!=bgdId && bgdId.length()>0){
			hql.append(" and t.bgdId="+bgdId);
		}else{
			return null;
		}
		hql.append(" order by t.yhType1,t.yhType2");
		return (List)this.utilDao.getHqlQuery(hql.toString());
	}

}
